package com.longxin.demoapp.experiments.experiment2.controller;


import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.longxin.demoapp.R;
import com.longxin.demoapp.experiments.experiment2.pojo.entity.Experiment2EntityUser;

public class Experiment2AddOrEditController extends Experiment2Controller {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.experiment_2_item_add_or_edit);

        Experiment2EntityUser user = (Experiment2EntityUser) getIntent().getSerializableExtra("data");

        boolean update = true;
        if (user != null) {
            EditText editTextUsername = (EditText) findViewById(R.id.experiment_2_item_text_add_or_edit_username);
            EditText editTextPhone = (EditText) findViewById(R.id.experiment_2_item_text_add_or_edit_phone);
            editTextUsername.setText(user.getUsername());
            editTextPhone.setText(user.getPhone());
        }else {
            user = new Experiment2EntityUser();
            update = false;
        }

        final Experiment2EntityUser finalUser = user;
        final boolean finalUpdate = update;
        findViewById(R.id.experiment_2_item_btn_add_or_edit_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String editTextUsername = ((EditText) findViewById(R.id.experiment_2_item_text_add_or_edit_username)).getText().toString();
                String editTextPhone = ((EditText) findViewById(R.id.experiment_2_item_text_add_or_edit_phone)).getText().toString();
                finalUser.setUsername(editTextUsername);
                finalUser.setPhone(editTextPhone);
                if (finalUpdate) {
                    service.update(finalUser, adapter);
                }else {
                    service.insert(finalUser, adapter);
                }

                service.changeActivity(Experiment2Controller.class);
            }
        });
    }
}